import requests
from lxml import etree
'''
需求
获取四大名著书的章节名称和url
'''
url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
}
res = requests.get(url, headers=headers)
tree = etree.HTML(res.content.decode())
li_list = tree.xpath('//div[@class="book-mulu"]/ul/li')
# print(li_list)
for li in li_list:
    # 章节的url
    print('https://www.shicimingju.com' + li.xpath('./a/@href')[0])
    # 章节的名称
    print(li.xpath('./a/text()')[0])
'''
/book/sanguoyanyi/119.html
https://www.shicimingju.com/book/sanguoyanyi/1.html
'''